<?php 
require_once $_SERVER["DOCUMENT_ROOT"]."/inc/core/env.inc.php";

$return["success"] = false;
$return["html"] = null;
$return["error"] = null;

if ($_USER->login && $_USER->asPermission('set_permissions')) {
	$return['success'] = true;
	$return['html'] = file_get_contents($_SITE['path']['dir']['blocks']['containers']['admin'].'/admin-rows.tag.html');
	
	//combo users
	$combo = file_get_contents($_SITE['path']['dir']['blocks']['containers']['admin'].'/admin-select-users.tag.html');
	$options = '';
	foreach ($_ADMIN->getAllUsers() as $ele) {
		if ($ele["id"] == $_POST["idu"]) {
			$select = ' selected';
		} else {
			$select = '';
		}
		
		$options .= '<option value="'.$ele["id"].'"'.$select.'>'.$ele["name"].' '.$ele["surname"].'</option>';
	}
	$combo = str_replace($_TAGS["admin-select-users-options"], $options, $combo);
	$combo = '<li>'.$combo.'</li>';
	
	
	// row permissions
	$rowPermission = file_get_contents($_SITE['path']['dir']['blocks']['containers']['admin'].'/admin-row.tag.html');
	$col = file_get_contents($_SITE['path']['dir']['blocks']['containers']['admin'].'/admin-col.tag.html');
	
	$altActive 		= $_TRANSLATE->traslate('{VOCABULARY|permission_active}', $_SESSION["LANGUAGE"]);
	$altDisactive 	= $_TRANSLATE->traslate('{VOCABULARY|permission_disactive}', $_SESSION["LANGUAGE"]);
	
	$icoActive 		= '<img data="{idu:'.$_POST["idu"].', idp: #idp, add: false}" src="/inc/templates/default/img/ico/admin/permission-true.gif" alt="'.$altActive.'" />';
	$icoDisactive 	= '<img data="{idu:'.$_POST["idu"].', idp: #idp, add: true}" src="/inc/templates/default/img/ico/admin/permission-false.gif" alt="'.$altDisactive.'" />';
	
	$rowClass = 'row-permission-set';
	$rows = '<li><div><b>Permissions set</b></div><div><b>Permission description</b></div><br class="clearboth"></li>';
	
	$sql = "SELECT * FROM ".$_DB->getPrefix()."permissions WHERE name != 'set_permissions'";
	$res = $_DB->query($sql);
	
	$userSet = $_USER->getPermissions4User($_POST["idu"]);
	
	foreach($res as $ele) {
			
		if (in_array($ele["name"], $userSet)) {
			$ico = str_replace("#idp", $ele["id"], $icoActive);
		} else {
			$ico = str_replace("#idp", $ele["id"], $icoDisactive);
		}
		
		$description = str_replace($_TAGS["share-content-col"], $ico, $col);
		$description .= str_replace($_TAGS["share-content-col"], $_TRANSLATE->traslate($ele["description"], $_SESSION["LANGUAGE"]), $col);
		
		$row = str_replace($_TAGS['admin-row-class'], $rowClass, $rowPermission);
		$rows .= str_replace($_TAGS['share-content-columns'], $description, $row);
		
	}
	
	$return['html'] = str_replace($_TAGS["admin-select-users"], $combo, $return['html']);
	$return['html'] = str_replace($_TAGS['admin-rows'], $rows, $return['html']);
	
} else {
	$return["error"] = $_TRANSLATE->traslate('{VOCABULARY|permission_disactive}', $_SESSION['LANGUAGE']);
}

echo '('.json_encode($return).')';
?>